package com.ruoyi.wisdom.event;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.tool.TimerUtils;
import com.ruoyi.wisdom.domain.IotConfig;
import com.ruoyi.wisdom.domain.IotPickDevice;
import com.ruoyi.wisdom.domain.swap.SwapPickDevice;
import com.ruoyi.wisdom.service.IIotPickDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

@Component
public class PickDeviceEvent extends IotEvent {

    @Autowired
    private IIotPickDeviceService iotPickDeviceService;

    /**
     * 数据处理
     *
     * @param swapPickDevice
     */
    public void handle(SwapPickDevice swapPickDevice) {
        if (swapPickDevice.getOperate() == null) {
            save(swapPickDevice);
        } else {
            switch (swapPickDevice.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapPickDevice);
                    break;
                default:
                    save(swapPickDevice);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapPickDevice
     */
    public void save(SwapPickDevice swapPickDevice) {
        IotPickDevice result = iotPickDeviceService.selectIotPickDeviceById(swapPickDevice.getId());
        // 实体装换
        IotPickDevice pickDevice = getModelMapper().map(swapPickDevice, IotPickDevice.class);
        if (result != null) {
            iotPickDeviceService.updateIotPickDevice(pickDevice);
        } else {
            iotPickDeviceService.insertIotPickDevice(pickDevice);
        }
    }

    /**
     * 删除
     *
     * @param swapPickDevice
     */
    public void delete(SwapPickDevice swapPickDevice) {
        iotPickDeviceService.deleteIotPickDeviceById(swapPickDevice.getId());
    }

    /**
     * 推送设备到平台
     */
    public void push() {
        IotPickDevice params = new IotPickDevice();
        params.setOnline(1);
        List<IotPickDevice> rows = iotPickDeviceService.selectIotPickDeviceList(params);
        if (rows.size() == 0) return;
        // 数据推送到平台
        for (IotPickDevice row : rows) {
            redisSecondTemplate.opsForList().rightPush(iotConstants.PUSH_DEVICE, JSONObject.toJSONString(row));
        }
    }

    /**
     * 设备巡检
     */
    public void patrol() {
        try {
            IotPickDevice params = new IotPickDevice();
            List<IotPickDevice> rows = iotPickDeviceService.selectIotPickDeviceList(params);
            if (rows.size() == 0) return;

            // // 默认五分钟巡检一次设备
            IotConfig iotConfig = iotConstants.getIotConfig();
            long timestamp = System.currentTimeMillis() - (60000 * iotConfig.getPatrolTime());

            for (IotPickDevice row : rows) {
                Long onlineTime = TimerUtils.dateFormatToTime(row.getOnlineTime(), "yyyy-MM-dd HH:mm:ss");
                System.out.println("当前时间戳：" + System.currentTimeMillis());
                if (row.getOnlineTime() == null) {
                    row.setOnline(-1);
                    iotPickDeviceService.updateIotPickDevice(row);
                    continue;
                }

                if (onlineTime < timestamp) {
                    row.setOnline(-1);
                } else {
                    row.setOnline(1);
                }
                iotPickDeviceService.updateIotPickDevice(row);
            }
        } catch (ArithmeticException e) {// 异常类型 异常类型的引用变量名
            System.out.println("设备巡检异常：" + e.getMessage());
        }
    }
}
